package com.example.take_out_dish.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.take_out_dish.config.JwtConfig;
import com.example.take_out_dish.dto.DishDTO;
import com.example.take_out_dish.dto.SetmealDishDto;
import com.example.take_out_dish.dto.SetmealDto;
import com.example.take_out_dish.entity.Setmeal;
import com.example.take_out_dish.entity.SetmealDish;
import com.example.take_out_dish.exceptions.BusinessException;
import com.example.take_out_dish.exceptions.enumetation.ResponseEnum;
import com.example.take_out_dish.mapper.SetmealDishMapper;
import com.example.take_out_dish.mapper.SetmealMapper;
import com.example.take_out_dish.service.SetmealDishService;
import com.example.take_out_dish.service.SetmealService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.take_out_dish.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 * 套餐 服务实现类
 * </p>
 *
 * @author yyyyys
 * @since 2024-07-01
 */
@Slf4j
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Resource
    private SetmealMapper setmealMapper;
    @Resource
    private SetmealDishMapper setmealDishMapper;
    @Resource
    private SetmealDishService setmealDishService;
    @Resource
    private HttpServletRequest request;
    @Resource
    private JwtConfig jwtConfig;

    @Override
    public Page<SetmealDto> pageWithCategoryName(int page, int pageSize, String name, String categoryId, Integer status) {
        Page<SetmealDto> pageResult = new Page<>(page, pageSize);
        List<SetmealDto> setmealDtoList = setmealMapper.selectSetmaelDTOPage(pageResult, name, categoryId, status);
        pageResult.setRecords(setmealDtoList);
        return pageResult;
    }

    @Override
    public void saveSetmeal(Setmeal setmeal) {
        String token = request.getHeader("token");
        if (!JwtUtils.validateToken(token,jwtConfig.getKey())){
            throw new BusinessException(ResponseEnum.TOKEN_EXPIRED);
        }
        Claims claims = JwtUtils.parseJwt(token, jwtConfig.getKey());
        String name = claims.get("name", String.class);
        setmeal.setCode("123");
        setmeal.setCreateUser(name);
        setmeal.setUpdateUser(name);
        setmeal.setCreateTime(new Date());
        setmeal.setUpdateTime(new Date());
        save(setmeal);
    }

    @Override
    public void addSetmealDishDto(SetmealDishDto setmealDishDto) {
        String token = request.getHeader("token");
        if (!JwtUtils.validateToken(token,jwtConfig.getKey())){
            throw new BusinessException(ResponseEnum.TOKEN_EXPIRED);
        }
        Claims claims = JwtUtils.parseJwt(token, jwtConfig.getKey());
        String name = claims.get("name", String.class);
        Random random = new Random();

        Setmeal setmeal = new Setmeal();
        setmeal.setId(generateRandom19DigitNumber(random));
        setmeal.setName(setmealDishDto.getName());
        setmeal.setCategoryId(setmealDishDto.getCategoryId());
        setmeal.setPrice(setmealDishDto.getPrice());
        setmeal.setStatus(setmealDishDto.getStatus());
        setmeal.setCode("2345");
        setmeal.setDescription(setmealDishDto.getDescription());
        setmeal.setImage(setmealDishDto.getImage());
        setmeal.setCreateTime(new Date());
        setmeal.setUpdateTime(new Date());
        setmeal.setCreateUser(name);
        setmeal.setUpdateUser(name);
        save(setmeal);

        for (SetmealDish dish : setmealDishDto.getSetmealDishes()) {
            SetmealDish setmealDish = new SetmealDish();
            setmealDish.setId(generateRandom19DigitNumber(random));
            setmealDish.setDishId(dish.getDishId());
            setmealDish.setSetmealId(setmeal.getId());
            setmealDish.setName(dish.getName());
            setmealDish.setPrice(dish.getPrice());
            setmealDish.setCopies(dish.getCopies());
            setmealDish.setCreateTime(new Date());
            setmealDish.setUpdateTime(new Date());
            setmealDish.setCreateUser(name);
            setmealDish.setUpdateUser(name);
            setmealDishService.save(setmealDish);

        }


    }

    @Override
    public void updateSetmealDishDto(SetmealDishDto setmealDishDto) {
        String token = request.getHeader("token");
        if (!JwtUtils.validateToken(token,jwtConfig.getKey())){
            throw new BusinessException(ResponseEnum.TOKEN_EXPIRED);
        }
        Claims claims = JwtUtils.parseJwt(token, jwtConfig.getKey());
        String name = claims.get("name", String.class);
        Random random = new Random();
        Setmeal setmeal = new Setmeal();
        Setmeal setmealOne = getById(setmealDishDto.getId());
        if (!Objects.equals(setmealOne.getName(), setmealDishDto.getName())) {
            setmealOne.setName(setmealDishDto.getName());
        } else {
            setmealOne.setName(null);
        }
        setmeal.setName(setmealDishDto.getName());
        setmeal.setCategoryId(setmealDishDto.getCategoryId());
        setmeal.setPrice(setmealDishDto.getPrice());
        setmeal.setStatus(setmealDishDto.getStatus());
        setmeal.setCode("2345");
        setmeal.setDescription(setmealDishDto.getDescription());
        setmeal.setImage(setmealDishDto.getImage());
        setmeal.setCreateTime(new Date());
        setmeal.setUpdateTime(new Date());
        setmeal.setCreateUser(name);
        setmeal.setUpdateUser(name);
        setmealMapper.updateById(setmeal);

        setmealDishMapper.deleteBySetmealId(setmealDishDto.getId());
        for (SetmealDish dish : setmealDishDto.getSetmealDishes()) {
            SetmealDish setmealDish = new SetmealDish();
            setmealDish.setId(generateRandom19DigitNumber(random));
            setmealDish.setDishId(dish.getDishId());
            setmealDish.setSetmealId(setmeal.getId());
            setmealDish.setName(dish.getName());
            setmealDish.setPrice(dish.getPrice());
            setmealDish.setCopies(dish.getCopies());
            setmealDish.setCreateTime(new Date());
            setmealDish.setUpdateTime(new Date());
            setmealDish.setCreateUser(name);
            setmealDish.setUpdateUser(name);
            setmealDishService.updateById(setmealDish);
        }



    }

    private static String generateRandom19DigitNumber(Random random) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < 19; i++) {
            int digit = random.nextInt(10); // 生成0-9的随机数字
            stringBuilder.append(digit);
        }
        return stringBuilder.toString();
    }
//    @Override
//    public void saveSetmeal(Setmeal setmeal) {
//        String token = request.getHeader("token");
//        if (!JwtUtils.validateToken(token,jwtConfig.getKey())){
//            throw new BusinessException(ResponseEnum.TOKEN_EXPIRED);
//        }
//        Claims claims = JwtUtils.parseJwt(token, jwtConfig.getKey());
//        String name = claims.get("name", String.class);
//        setmeal.setCreateUser(name);
//        setmeal.setUpdateUser(name);
//        setmeal.setCreateTime(new Date());
//        setmeal.setUpdateTime(new Date());
//        save(setmeal);
//    }

}
